package com.sicnu.history.mapper;

import com.sicnu.history.bean.entity.HistoryDO;
import com.sicnu.history.bean.entity.HistoryEQ;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 29718
 */
public interface HistoryMapper {

    // 添加历史记录
    @Insert("INSERT INTO history (resource_id, user_id, histories_status) VALUES (#{history.resourceId}, #{history.userId}, #{history.historiesStatus})")
    void addHistory(@Param("history") HistoryEQ history);

    // 删除历史记录
    @Delete("DELETE FROM history WHERE resource_id = #{resourceId} AND user_id = #{userId} AND histories_status = #{historiesStatus}")
    void removeHistory(@Param("resourceId") Integer resourceId,@Param("userId") Long userId,@Param("historiesStatus") Integer historiesStatus);

    // 查询用户的所有历史记录
    @Select("SELECT * FROM history WHERE user_id = #{userId}")
    List<HistoryDO> getUserHistories(@Param("userId") Long userId);

//    // 查询指定资源的历史记录数量
//    @Select("SELECT COUNT(*) FROM history WHERE resource_id = #{resourceId} AND histories_status = #{historiesStatus}")
//    Long getHistoryCount(@Param("resourceId") Long resourceId);

    // 检查是否存在特定用户和资源的历史记录
    @Select("SELECT COUNT(*) > 0 FROM history WHERE user_id = #{userId} AND resource_id = #{resourceId} AND histories_status = #{historiesStatus}")
    boolean existsByUserIdAndResourceId(@Param("userId") Long userId, @Param("resourceId") Long resourceId);

    // 更新历史记录状态
    @Update("UPDATE history SET histories_status = #{historiesStatus} WHERE user_id = #{userId} AND resource_id = #{resourceId}")
    void updateHistoryStatus(@Param("userId") Long userId, @Param("resourceId") Long resourceId, @Param("historiesStatus") Integer historiesStatus);

    @Select("SELECT COUNT(*) > 0 FROM history WHERE resource_id = #{resourceId} AND user_id = #{userId}")
    Boolean existsByResourceIdAndUserId(@Param("resourceId") Integer resourceId, @Param("userId") Long userId);
}